﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Core.Algorithm;

namespace Microhard.Permission.Models
{
    public class SetDictionaryFiledName
    {
        public static void SetDicFiledTxtName<T>(ref T model)
        {
            var dic = AlgCompare.GetDicTxtNameWithDicFieldValue(model);
            if (dic.Count > 0)
            {
                DictionaryCache dc = new DictionaryCache();
                Type t = model.GetType();
                foreach (var d in dic)
                {
                    Guid guidValue = Guid.Empty;
                    if (Guid.TryParse(d.Value, out guidValue))
                    {
                        if (guidValue != Guid.Empty)
                        {
                            var dm = dc.GetFromCache(d.Value);
                            if (dm != null)
                            {
                                var txtf = t.GetProperty(d.FieldName);
                                txtf.SetValue(model, dm.Name, null);
                            }
                        }
                    }
                    else
                    {
                        PCDCache pcd = new PCDCache();
                        var pcdModel = pcd.GetSingle(d.PCDType, d.Value);
                        if (pcdModel != null)
                        {
                            var txtf = t.GetProperty(d.FieldName);
                            txtf.SetValue(model, pcdModel.Name, null);
                        }
                    }
                }
            }
        }
    }
}
